<?php
Yii::import('application.extensions.PHPMailer.AqSendMail');
class OnlineAffidavitController extends Controller {

    public function actionIndex() {
        $this->layout = 'innerpage';
        $model = new StudentDetails();
        $amanStudentParentsDetail = new StudentParentsDetails();
        $amanStudentCollegeDetails = new StudentCollegeDetails();
        $amanStudentCourseDetails = new StudentCourseDetails();
        $amanStudentCollegeDetails->gender = 1;
        $model->gender = 1;
        $this->render('index', array(
            'model' => $model,
            'amanStudentParentsDetail' => $amanStudentParentsDetail,
            'amanStudentCollegeDetails' => $amanStudentCollegeDetails,
            'amanStudentCourseDetails' => $amanStudentCourseDetails,
        ));
    }

    public function actionstep1() {
        $this->layout = 'innerpage';
        $this->render('step1');
    }

    public function actionStep2() {
        $this->layout = 'innerpage';
        $model = new StudentDetails();
        $model->gender = 1;
        if (Yii::app()->getUser()->hasState('StudentDetails')) {
            $model->setAttributes(Yii::app()->user->getState('StudentDetails'));
        }

        if (isset($_POST['StudentDetails'])) {
            $model->setAttribute('confirm_email_address', $_POST['StudentDetails']['confirm_email_address']);
            $model->setAttributes($_POST['StudentDetails']);
            if ($model->validate()) {
                Yii::app()->user->setState('StudentDetails', $_POST['StudentDetails']);
                Yii::app()->user->setState('step1', 'step1');
                $this->redirect(array('onlineAffidavit/step3'));
            }
        }
        $this->render('step2', array('model' => $model));
    }

    public function actionStep3() {
        $this->layout = 'innerpage';
        $StudentParentsDetails = new StudentParentsDetails();
        $step1=Yii::app()->user->hasState('step1');
        if (!empty($step1)) {
            if (Yii::app()->getUser()->hasState('StudentParentsDetails')) {
                $StudentParentsDetails->setAttributes(Yii::app()->user->getState('StudentParentsDetails'));
            }

            if (isset($_POST['StudentParentsDetails'])) {
                $StudentParentsDetails->setAttributes($_POST['StudentParentsDetails']);
                if ($StudentParentsDetails->validate()) {
                    Yii::app()->user->setState('StudentParentsDetails', $StudentParentsDetails->getAttributes());
                    Yii::app()->user->setState('step2', 'step2');
                    $this->redirect(array('onlineAffidavit/step4'));
                }
            }

            $this->render('step3', array('StudentParentsDetails' => $StudentParentsDetails));
        } else {
            $this->redirect(array('onlineAffidavit/step2'));
        }
    }

    public function actionStep4() {
        $this->layout = 'innerpage';
        $StudentCollegeDetails = new StudentCollegeDetails();
        $StudentCollegeDetails->gender = 1;
        $step2=Yii::app()->user->hasState('step2');
        if (!empty($step2)) {
            if (Yii::app()->getUser()->hasState('StudentCollegeDetails')) {
                $StudentCollegeDetails->setAttributes(Yii::app()->user->getState('StudentCollegeDetails'));
            }

            if (isset($_POST['StudentCollegeDetails'])) {
                $StudentCollegeDetails->setAttributes($_POST['StudentCollegeDetails']);
                if ($StudentCollegeDetails->validate()) {
                    Yii::app()->user->setState('StudentCollegeDetails', $StudentCollegeDetails->getAttributes());
                    Yii::app()->user->setState('step3', 'step3');
                    $this->redirect(array('onlineAffidavit/step5'));
                }
            }
            $this->render('step4', array('StudentCollegeDetails' => $StudentCollegeDetails));
        } else {
            $this->redirect(array('onlineAffidavit/step3'));
        }
    }

    public function actionStep5() {
        $this->layout = 'innerpage';
        $StudentCourseDetails = new StudentCourseDetails();
        $step3=Yii::app()->user->hasState('step3');
        if (!empty($step3)) {
            if (Yii::app()->getUser()->hasState('StudentCourseDetails')) {
                $StudentCourseDetails->setAttributes(Yii::app()->user->getState('StudentCourseDetails'));
            }

            if (isset($_POST['StudentCourseDetails'])) {
                $StudentCourseDetails->setAttributes($_POST['StudentCourseDetails']);
                if ($StudentCourseDetails->validate()) {
                    Yii::app()->user->setState('StudentCourseDetails', $_POST['StudentCourseDetails']);
                    Yii::app()->user->setState('step4', 'step4');
                    $this->redirect(array('onlineAffidavit/step6'));
                }
            }
            $this->render('step5', array('StudentCourseDetails' => $StudentCourseDetails));
        } else {
            $this->redirect(array('onlineAffidavit/step4'));
        }
    }

    public function actionStep6() {
        $this->layout = 'innerpage';
        include( Yii::getPathOfAlias('webroot') . '/tcpdf/HTML2PDF.php');
        $step4=Yii::app()->user->hasState('step4');
        if (!empty($step4)) {
            if (isset($_POST['savedata'])) {
                $amanStudentDetails = new StudentDetails();
                $amanStudentDetails->setAttributes(Yii::app()->user->getState('StudentDetails'));
                if ($amanStudentDetails->save(false)) {
                    $refrenceid=refrenceNoFormat() . $amanStudentDetails->id;
                    StudentDetails::model()->updateByPk($amanStudentDetails->id, array('refrence_no' => $refrenceid));
                    $amanStudentParentsDetails = new StudentParentsDetails();
                    $amanStudentParentsDetails->setAttributes(Yii::app()->user->getState('StudentParentsDetails'));
                    $amanStudentParentsDetails->setAttribute('student_details_id', $amanStudentDetails->id);
                    $amanStudentParentsDetails->save(false);
//

                    $amanStudentCollegeDetails = new StudentCollegeDetails();
                    $amanStudentCollegeDetails->setAttributes(Yii::app()->user->getState('StudentCollegeDetails'));
                    $amanStudentCollegeDetails->setAttribute('student_details_id', $amanStudentDetails->id);
                    $amanStudentCollegeDetails->save(false);
//

                    $amanStudentCourseDetails = new StudentCourseDetails();
                    $amanStudentCourseDetails->setAttributes(Yii::app()->user->getState('StudentCourseDetails'));
                    $amanStudentCourseDetails->setAttribute('student_details_id', $amanStudentDetails->id);
                    $amanStudentCourseDetails->save(false);
                    Yii::app()->user->setState('StudentDetails', '');
                    Yii::app()->user->setState('StudentParentsDetails', '');
                    Yii::app()->user->setState('StudentCollegeDetails', '');
                    Yii::app()->user->setState('StudentCourseDetails', '');
                    Yii::app()->user->setState('step5', 'step5');
                    ob_start();
                    echo $this->renderPartial('_student-affidavit_pdf', array('amanStudentDetails' => $amanStudentDetails));
                    $content = ob_get_clean();

                    try {
                        $html2pdf = new HTML2PDF('P', 'A4', 'en');
                        $html2pdf->setDefaultFont('helvetica');
                        $html2pdf->writeHTML($content, false);
                        $html2pdf->Output("student-pdf/student-affidavit.pdf", 'F');
                    } catch (HTML2PDF_exception $e) {
                        echo $e;
                        exit;
                    }

                    ob_start();
                    echo $this->renderPartial('_parent-affidavit_pdf', array('amanStudentDetails' => $amanStudentDetails, 'amanStudentParentsDetails' => $amanStudentParentsDetails));
                    $content = ob_get_clean();

                    try {
                        $html2pdf = new HTML2PDF('P', 'A4', 'en');
                        $html2pdf->setDefaultFont('helvetica');
                        $html2pdf->writeHTML($content, false);
                        $html2pdf->Output("student-pdf/parent-affidavit.pdf", 'F');
                    } catch (HTML2PDF_exception $e) {
                        echo $e;
                        exit;
                    }

                    $adminSubject = ' Online Anti Ragging Affidavits ';
                    $adminTpl = 'student_afidavit.php';
                    $username=$amanStudentDetails->first_name . ' ' . $amanStudentDetails->last_name;
                    $this->sendEmail($username, $amanStudentDetails->email_address, $adminSubject, $adminTpl);

                    $this->redirect(array('onlineAffidavit/step7'));
                }
            }
            $this->render('step6');
        } else {
            $this->redirect(array('onlineAffidavit/step5'));
        }
    }

    public function actionStep7() {
        $this->layout = 'innerpage';
        $step5=Yii::app()->user->hasState('step5');
        if (!empty($step5)) {
            Yii::app()->user->setState('step1', '');
            Yii::app()->user->setState('step2', '');
            Yii::app()->user->setState('step3', '');
            Yii::app()->user->setState('step4', '');
            Yii::app()->user->setState('step5', '');
            $this->render('step7');
        } else {

            $this->redirect(array('onlineAffidavit/step6'));
        }
    }

    public function sendEmail($userName, $to, $subject, $tpl) {
        $emalDir = aqConfig::getEmailDir();
        $mail['to'] = $to;
        $mail['subject'] = $subject;
        $mail['msgtpl'] = $emalDir . $tpl;
        $mailReplaceArr = array('$USER_NAME' => $userName);
        $parentaffidavit = 'parent-affidavit';
        $studentaffidavit='student-affidavit';
         $parentachmentFile= Yii::getPathOfAlias('webroot') . '/student-pdf/parent-affidavit.pdf';
        $studentattachmentFile = Yii::getPathOfAlias('webroot') . '/student-pdf/student-affidavit.pdf';
        $filename=$parentaffidavit.','.$studentaffidavit;
        AqSendMail::EmailSend($to,$mail, $mailReplaceArr,$parentachmentFile,$studentattachmentFile,$filename,$parentaffidavit,$studentaffidavit);
    }

}
